package com.dflc.utils;

import com.baomidou.mybatisplus.core.metadata.OrderItem;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * TODO lb <类名>
 *
 * @author tqx
 * @version 1.0.0.0
 * <p>Title: EntityUtils </p >
 * <p>Description: TODO tqx </p >
 * Package: com.dflc.utils
 * Company: 数字涪陵
 * Copyright: Copyright(c) 产品研发与安全部
 * Date: 2023年03月07日 16:49
 */
public class EntityUtils {

    /**
     * @param object
     * @return
     * @description: 实体类转Map
     * @author: Jeff
     * @date: 2019年10月29日
     */
    public static Map<String, Object> entityToMap(Object object) {
        Map<String, Object> map = new HashMap<>();
        for (Field field : object.getClass().getDeclaredFields()) {
            try {
                boolean flag = field.isAccessible();
                field.setAccessible(true);
                Object o = field.get(object);
                map.put(field.getName(), o);
                field.setAccessible(flag);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return map;
    }

    public static Map<String, String> entityToMap2(Object object) {
        Map<String, String> map = new HashMap<>();
        for (Field field : object.getClass().getDeclaredFields()) {
            try {
                boolean flag = field.isAccessible();
                field.setAccessible(true);
                String o = String.valueOf(field.get(object));
                map.put(field.getName(), o);
                field.setAccessible(flag);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return map;
    }

    /**
     * @param <T>
     * @param map    需要初始化的数据，key字段必须与实体类的成员名字一样，否则赋值为空
     * @param entity 需要转化成的实体类
     * @return
     * @description: Map转实体类
     * @author: Jeff
     * @date: 2019年10月29日
     */
    public static <T> T mapToEntity(Map<String, Object> map, Class<T> entity) {
        T t = null;
        try {
            t = entity.newInstance();
            for (Field field : entity.getDeclaredFields()) {
                if (map.containsKey(field.getName())) {
                    boolean flag = field.isAccessible();
                    field.setAccessible(true);
                    Object object = map.get(field.getName());
                    if (object != null && field.getType().isAssignableFrom(object.getClass())) {
                        field.set(t, object);
                    }
                    field.setAccessible(flag);
                }
            }
            return t;
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return t;
    }

    public static <T> List<T> objToList(Object obj, Class<T> cla) {
        List<T> list = new ArrayList<T>();
        if (obj instanceof ArrayList<?>) {
            for (Object o : (List<?>) obj) {
                list.add(cla.cast(o));
            }
            return list;
        }
        return null;
    }

    public static List<OrderItem> sort(PageRequestEntity page) {
        List<OrderItem> orderItemList = page.getSort().stream()
                .map(sortEntity -> {
                    OrderItem item = new OrderItem();
                    item.setColumn(sortEntity.getField());
                    item.setAsc(String.valueOf(sortEntity.getType()).equals("asc"));
                    return item;
                })
                .collect(Collectors.toList());
        return orderItemList;
    }


    public static List<Integer> strToInt(List<String> strings) {
        List<Integer> list = new ArrayList<>();
        for (String s : strings) {
            list.add(Integer.valueOf(s));
        }
        return list;
    }
    public static List<OrderItem> buildSort(String field,Boolean asc) {
        List<OrderItem> orderItemList = new ArrayList<>();
        OrderItem orderItem = new OrderItem();
        orderItem.setColumn(field);
        orderItem.setAsc(asc);
        orderItemList.add(orderItem);
        return orderItemList;
    }

}
